using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;

public class SetCanvasBounds : MonoBehaviour {

// #if UNITY_IOS
// 	[DllImport("__Internal")]
//     private extern static void GetSafeAreaImpl(out float x, out float y, out float w, out float h);
// #endif

	private Rect GetSafeArea()
	{
	 
		return new Rect(0, 0, Screen.width, Screen.height);
	}

    private Camera myCamera;
	Rect lastSafeArea = new Rect(0, 0, 0, 0);

	// Use this for initialization
	void Start () {
        myCamera = this.transform.GetComponent<Camera>();

		Rect safeArea = GetSafeArea(); // or Screen.safeArea if you use a version of Unity that supports it

		if (safeArea != lastSafeArea) {
			ApplySafeArea (safeArea);
			lastSafeArea = safeArea;
		}
	}

	void ApplySafeArea(Rect area)
	{
		if(myCamera != null)
        {
			myCamera.pixelRect = area;
        }
	}

	// Update is called once per frame
	void Update () 
	{
		Rect safeArea = GetSafeArea(); // or Screen.safeArea if you use a version of Unity that supports it

		if (safeArea != lastSafeArea) {
			ApplySafeArea (safeArea);
			lastSafeArea = safeArea;
		}
	}
}
